package com.partners.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
public class JsonUtils {
    public static String toJson(Object obj) {
        Gson gson = new GsonBuilder()
            .setPrettyPrinting()
            .create();
        return gson.toJson(obj);
    }


    /**
     * 是否按照月份显示
    * */
    public static Map<Object, List<Map<String, Object>>> toMonthList(List<Map<String,Object>> list){
        for (Map<String,Object> map : list){
            String dateFormat = "yyyy-MM";
            String format = TimestampUtils.getDateTimeOfTimestamp((Integer) map.get("addtime"),dateFormat);
            map.put("format",format);
            //新增
            String timeFormat = "yyyy-MM-dd HH:mm:ss";
            String addtime = TimestampUtils.getDateTimeOfTimestamp((Integer) map.get("addtime"),timeFormat);
            map.put("addtime",addtime);
        }
        final Map<Object, List<Map<String, Object>>> timeList = list.stream().collect(Collectors.groupingBy(i -> i.get("format")));
            TreeMap<Object, List<Map<String, Object>>> treeMap = new TreeMap<>();
            treeMap.putAll(timeList);
            NavigableMap<Object, List<Map<String, Object>>> descendingMap = treeMap.descendingMap();
        return descendingMap;
    }


}
